#include "gamemainwindow.h"
#include "ui_gamemainwindow.h"
#include <QDebug>
#include <QTextDocument>
#include <QTimer>
GameMainWindow::GameMainWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GameMainWindow)
{
    Init();
}

GameMainWindow::~GameMainWindow()
{
    delete ui;
    delete editor;
    delete timer;
}

void GameMainWindow::SetName(QString name)
{
    ui->DialogName->setText(name);
    QTextDocument doc;
    doc.setHtml(name);
    QString tmp = doc.toPlainText();
    if(tmp.isEmpty())
    {
        ui->DialogName->hide();
    }
    else
        ui->DialogName->show();
}

void GameMainWindow::SetDialogText(QString text)
{
    QTextDocument doc;
    doc.setHtml(text);
    QString tmp = doc.toPlainText();
    if(tmp.isEmpty())
    {
        ui->Dialog->hide();
        return;
    }
    else
    {
        ui->Dialog->show();
    }
    ui->Dialog->setText("");
    if(!EventCenter::Instance().GetApplicationMode())
    {
        ui->Dialog->setText(text);
    }
    else
    {
        if(!timer)
            timer = new QTimer();
        this->text = text;
        timer->stop();
        timer->start(time);
    }
}

void GameMainWindow::resizeEvent(QResizeEvent *event)
{
    ui->Dialog->setMinimumHeight(this->height() * 0.25f);
    ui->Dialog->setMaximumHeight(this->height() * 0.25f);
}

void GameMainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
    if(!EventCenter::Instance().GetApplicationMode())
    {
        QString name = ui->DialogName->text();
        QString text = ui->Dialog->text();
        editor->Open(name,text);
    }
}

void GameMainWindow::Init()
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    editor = new DialogEditor();
    connect(&EventCenter::Instance(),&EventCenter::TextEdit,this,[=](QString& name,QString& text){
        SetName(name);
        SetDialogText(text);
    });
    ui->DialogName->SetSelectedDisabled(true);
    ui->Dialog->SetSelectedDisabled(true);
    ui->MenuBtn->SetSelectedDisabled(true);
    ui->DialogName->info = UILib::Instance().labels["游戏-角色名"];
    ui->DialogName->Refresh(ui->DialogName->info);
    ui->Dialog->info = UILib::Instance().labels["游戏-对话框"];
    ui->Dialog->Refresh(ui->Dialog->info);
    ui->MenuBtn->info = UILib::Instance().btns["菜单按钮"];
    ui->MenuBtn->Refresh(ui->MenuBtn->info);

    //逐字跳出
    timer = new QTimer();
    connect(timer,&QTimer::timeout,this,&GameMainWindow::ShowText);
    connect(&EventCenter::Instance(),&EventCenter::ChangeTextV,[=](int v){
        time = v;
    });
}

void GameMainWindow::ShowText()
{
    QTextDocument doc;
    static int htmlCount = 0;
    int nowTextCount = 0;
    QString plainText = "";
    if(!ui->Dialog->text().isEmpty())
    {
        doc.setHtml(ui->Dialog->text());
        nowTextCount = doc.toPlainText().count();
        plainText = doc.toPlainText();
    }

    while(plainText.isEmpty() || plainText.count() < nowTextCount + 1)
    {
        QString tmp = text.mid(0,htmlCount++);
        doc.setHtml(tmp);
        plainText = doc.toPlainText();
    }
    ui->Dialog->setText(doc.toHtml());

    doc.setHtml(text);
    int total = doc.toPlainText().count();
    if(plainText.count() >= total)
    {
        timer->stop();
        htmlCount = 0;
    }
}
